home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: bristol.com!handel!dan
- From: dan@bristol.com (J. Daniel Smith)
- Subject: Re: Q: realloc->free?
- Sender: usenet@bristol.com (USENET News System)
- Organization: Bristol Technology Inc.
- References: <4df2ud$706@oxy.rust.net> <4dgic7$qin@unix.sri.com>
- Message-ID: <dan.821890778@handel>
- Date: Wed, 17 Jan 1996 14:59:38 GMT
-
- In <4dgic7$qin@unix.sri.com> mklenk@updike.sri.com (Mark Klenk) writes:
- >Earl Bennett wrote:
- >>
- >>realloc() will free the old block. It is perfectly legal to say
- >>
- >> a = realloc(a, newsize);
- >>
- >>No memory loss should occur from this.
- > Excuse me??? What about if realloc fails?!
- > Then you've lost at least the old number of bytes,
- >
- > You should ALWAYS do this instead:
- >
- > b = realloc(a, newsize);
- > if (b == NULL) {
- > /* At least we haven't lost 'a'. */
- > }
-
- While this is indeed correct, ALWAYS having to write this can make for
- ugly code.
-
- Let's face it: under normal circumstances, realloc() is not going to
- fail; adding code to deal for the extremely rare case can add
- significantly to the complexity of the code. And if realloc() does
- fail, there are probably a lot bigger things to worry about than
- leaking memory (like the GUI not being able to create a window to tell
- you something has went wrong).
-
- Yes, the above code is more robust than simply
- a = realloc(a, newsize);
- but this marginal increase in robustness (given the chances of
- realloc() failing in normal operating conditions) needs to be
- evaluated against the added complexity of dealing with the failure.
- Yes, the programmer needs to be aware of the fact that realloc() can
- fail, but in some cases ALWAYS checking the return value of realloc()
- just isn't practical given other constraints.
-
- C++ is able to deal with this a bit more elgantly via exceptions.
-
- Dan
- --
- --------------------- message is author's opinion only --------------------
- J. Daniel Smith <DanS@bristol.com> http://www.bristol.com/~dan
- Bristol Technology Inc. +1 203 438 6969, 438-5013 (FAX)
- Ridgefield, Connecticut (USA) {info,jobs}@bristol.com
-